/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import Control.SMA;
import Entidad.Reporte;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

/**
 *
 * @author TOSHIBA
 */
public class main extends javax.swing.JFrame {

    /**
     * Creates new form main
     */
    
    int click=0;
    int largo=0,largou=0;
    String array1[]=new String[100];
    JOptionPane jop = new JOptionPane();

    ArrayList<Reporte> reportes1 = new ArrayList<>();
    ArrayList<Reporte> prono1 = new ArrayList<>();
    
    public main() {
        initComponents();
        jtext2.setText("5");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtext1 = new javax.swing.JTextField();
        jtext2 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        dlast = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        fore = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        area = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setLayout(null);

        jLabel1.setText("Weather Report");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(90, 60, 120, 20);

        jLabel2.setText("Values to Forecast");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(90, 100, 120, 20);
        jPanel1.add(jtext1);
        jtext1.setBounds(260, 50, 170, 30);
        jPanel1.add(jtext2);
        jtext2.setBounds(260, 90, 170, 30);

        jButton1.setText("Add");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);
        jButton1.setBounds(90, 153, 80, 30);

        dlast.setText("Delete Last");
        dlast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dlastActionPerformed(evt);
            }
        });
        jPanel1.add(dlast);
        dlast.setBounds(190, 153, 110, 30);

        jButton3.setText("Delete all");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton3);
        jButton3.setBounds(320, 153, 110, 30);

        fore.setText("Forecast");
        fore.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                foreActionPerformed(evt);
            }
        });
        jPanel1.add(fore);
        fore.setBounds(320, 200, 110, 30);

        area.setColumns(20);
        area.setRows(5);
        jScrollPane1.setViewportView(area);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(10, 245, 470, 350);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 502, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        area.setText("");// TODO add your handling code here:
        for(int l=0;l<100;l++){
        array1[l]=null;
        click=0;
        largo=0;
        largou=0;
        
}
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       String uno ;
       double prueba;
       uno= jtext1.getText();

       try {
       prueba= Double.parseDouble(uno);
       } catch (Exception exz) {
       JOptionPane.showMessageDialog(this, "Introduzca un valor de reporte valido", "reporte", JOptionPane.ERROR_MESSAGE);
       return;
}
reportes1.add(new Reporte(uno, null));

if (uno.length()!=0){
largo=largo+uno.length()+2;
largou=uno.length();
array1[click]=uno;
area.append(array1[click]+", ");
click++;
jtext1.setText(null);}
       
    }//GEN-LAST:event_jButton1ActionPerformed

    private void dlastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dlastActionPerformed
        
        if (array1[0]!=null){
            Document document = area.getDocument();
            largo=largo-2-array1[click-1].length();

            try {
            document.remove(largo, array1[click-1].length()+2);
            reportes1.remove(click-1);
            array1[click-1]=null;
            click--;

            } catch (BadLocationException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
}
    }//GEN-LAST:event_dlastActionPerformed

    @SuppressWarnings("static-access")
    private void foreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_foreActionPerformed
        // TODO add your handling code here:
        String n= jtext2.getText();

        if (array1[5]==null){
        jop.showMessageDialog(this, "Introduzca mas repotes", "das", JOptionPane.ERROR_MESSAGE);
        }
        else{
            
        int dias=  Integer.parseInt(n);
        int dia=1;
        SMA repor;
        repor=new SMA(reportes1, Integer.parseInt(jtext2.getText()));
        prono1=repor.determinarPronosticos();
        area.append("\n"+"\n");
        
        area.append("Algoritmo SMA"+"\n"+"\n");
        
        for (int i=1;i<=dias;i++){
        area.append("Día "+ dia+ "  "+  String.valueOf(prono1.get( i ).getTemperatura())+"\n");
        dia++;}
        
        }
    }//GEN-LAST:event_foreActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea area;
    private javax.swing.JButton dlast;
    private javax.swing.JButton fore;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jtext1;
    private javax.swing.JTextField jtext2;
    // End of variables declaration//GEN-END:variables
}
